const cloud = require('wx-server-sdk');
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
const db = cloud.database();

// 根据搜索关键词返回相应搜索数据
// const MAX_LIMIT = 100
exports.main = async (event, context) => {
  // const countResult = await db.collection('hotelList').count()
  if(event.switchType === 'scenic') {
    const res = await db.collection('scenicList').where({
      scenicName: db.RegExp({
        regexp: '.*' + event.searchKeyWord,
        options: 'i'
      })
    }).get()
    return res
  }else if(event.switchType === 'hotel') {
    const res = await db.collection('hotelList').where({
      hotelName: db.RegExp({
        regexp: '.*' + event.searchKeyWord,
        options: 'i'
      })
    }).get()
    return res
  }else if(event.switchType === 'good') {
    const res = await db.collection('goodList').where({
      goodName: db.RegExp({
        regexp: '.*' + event.searchKeyWord,
        options: 'i'
      })
    }).get()
    return res
  }else if(event.switchType === 'road') {
    const res = await db.collection('roadList').where({
      roadName: db.RegExp({
        regexp: '.*' + event.searchKeyWord,
        options: 'i'
      })
    }).get()
    return res
  }
  
  // if(res.result === undefined) {
  //   return {
  //     data: [],
  //   }
  // }
  // return res
  // console.log(res, 111);
  // let total = res.result && res.result.data.length
  // // console.log(total);
  // // const total = countResult.total
  // const batchTimes = Math.ceil(total / 100)
  // const tasks = []
  // for (let i = 0; i < batchTimes; i++) {
  //   const promise = db.collection('scenicList').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
  //   tasks.push(promise)
  // }
  // return (await Promise.all(tasks)).reduce((acc, cur) => {
  //   return {
  //     data: acc.data.concat(cur.data),
  //     errMsg: acc.errMsg,
  //   }
  // })
}